<template>
  <v-main>
    <v-container>
      <v-row align="center" justify="center" class="mt-15">
        <v-spacer></v-spacer>
        <v-col cols="auto">
          <v-avatar size="160">
            <v-img width="160px" height="160px" src="@/assets/logo-128.jpg"></v-img>
          </v-avatar>
        </v-col>
        <v-spacer></v-spacer>
      </v-row>
      <v-row align="center" justify="center" class="mt-15">
        <v-col cols="7" class="mt-10">
          <v-btn color="primary" rounded x-large width="100%" to="Register" replace>注册</v-btn>
        </v-col>
        <v-col cols="7" class="mt-5">
          <v-btn color="secondary" rounded x-large width="100%" @click="gotoLogin" replace>登录</v-btn>
        </v-col>
      </v-row>
    </v-container>
  </v-main>
</template>

<script>
export default {
  name: "Opening",
  beforeMount() {
    // 验证用户登录
    this.$store.commit("loadToken")
    if (this.$store.getters.hasLogin){
      this.$http.updateAuth()
      this.$http.post(
          'user/flush',
          {}
      ).then((res)=>{
        this.$store.commit('init',res.data)
        this.$store.commit('setToken',res.data.token)
        this.$http.updateAuth()

        // 跳转
        if (this.$route.query.redirect) {
          this.$router.replace("/home/index?redirect=" + this.$route.query.redirect)
        }else{
          this.$router.replace('/home/index')
        }
      }).catch((err)=>{
        this.$store.commit('quit')
      })

    }else{
      this.$store.commit('quit')
    }
  },
  methods:{
    gotoLogin(){
      if (this.$route.query.redirect){
        this.$router.push('/login?redirect='+this.$route.query.redirect)
      }else{
        this.$router.push('/login')
      }

    }
  }
}

</script>

<style scoped>

</style>
